package com.kmxd.ams.core.exception;

import com.kmxd.ams.core.util.AmsCallerUtil;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Objects;

/**
 * The type Record info.
 *
 * @author nickbi
 * @since 2022 /1/24
 */
public final class LogInfo implements Serializable {

  private final String className;
  private final String methodName;

  private LogInfo(final String className, final String methodName) {
    this.className = className;
    this.methodName = methodName;
  }

  /**
   * New instance record info.
   *
   * @param className the class name
   * @param methodName the method name
   * @return the record info
   */
  public static LogInfo newInstance(
      @NotNull final String className, @NotNull final String methodName) {
    return new LogInfo(className, methodName);
  }

  /**
   * 注意：需要明确以下自动获取的是否是抛出异常的对应层
   *
   * @return the record info
   */
  public static LogInfo newInstance() {
    final String callerMethodName = AmsCallerUtil.getCallerMethodName(3, false);
    return new LogInfo(AmsCallerUtil.getCallerName(3), callerMethodName);
  }

  public static LogInfo newCallerInstance() {
    final String callerMethodName = AmsCallerUtil.getCallerMethodName(4, false);
    return new LogInfo(AmsCallerUtil.getCallerName(4), callerMethodName);
  }

  /**
   * Gets class name.
   *
   * @return the class name
   */
  public String getClassName() {
    return className;
  }

  /**
   * Gets method name.
   *
   * @return the method name
   */
  public String getMethodName() {
    return methodName;
  }

  @Override
  public boolean equals(final Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof LogInfo)) {
      return false;
    }
    final LogInfo that = (LogInfo) o;
    return className.equals(that.className) && methodName.equals(that.methodName);
  }

  @Override
  public int hashCode() {
    return Objects.hash(className, methodName);
  }
}
